#import "BasicDownloadImgView.h"


@implementation BasicDownloadImgView

- (void)setImageURL:(NSString *)url
{
    _imageURL = url;
    
    [self sd_setImageWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:_placeholderImageName] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if(!error){
            if([self.strType isEqual:@"gaosi"]){
                self.contentMode = UIViewContentModeScaleAspectFill;
                self.clipsToBounds = YES;
                self.image= [self blurryImage:image withBlurLevel:2.0f];
            }else{
                if([self.strType isEqual:@"model"]){
                    self.contentMode = UIViewContentModeCenter;
                    if (image.size.width>image.size.height) {
//                       self.image = [self OriginImage:image scaleToSize:CGSizeMake(self.size.width, image.size.height/image.size.width*self.size.width)];
                    }else{
//                         self.image = [self OriginImage:image scaleToSize:CGSizeMake(image.size.width/image.size.height*self.size.width, self.size.width)];
                    }
                    self.image = image;
                }else if([self.strType isEqual:@"modelHC"]){
                    self.contentMode = UIViewContentModeCenter;
//                    self.image = [self OriginImage:image scaleToSize:CGSizeMake(self.size.height*image.size.width/image.size.height, self.size.height)];
                    //                    self.image = image;
                }
                else{
                    self.contentMode = UIViewContentModeScaleToFill;
                    self.image = image;
                }
            }
        }else{
            self.image = [UIImage imageNamed:self->_placeholderImageName];
        }
    }];
}

//获取新尺寸图片
- (UIImage*) OriginImage:(UIImage *)image scaleToSize:(CGSize)size
{
//    if([[UIScreen mainScreen] scale] == 2.0) {
        UIGraphicsBeginImageContextWithOptions(size, NO, 2.0);
//    } else {
//        UIGraphicsBeginImageContext(size);
//    }
    
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return scaledImage;   //返回的就是已经改变的图片
}

- (UIImage *)blurryImage:(UIImage *)image
           withBlurLevel:(CGFloat)blur {
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"
                                  keysAndValues:kCIInputImageKey, inputImage,
                        @"inputRadius", @(blur),
                        nil];
    
    CIImage *outputImage = filter.outputImage;
    
    CGImageRef outImage = [context createCGImage:outputImage
                                             fromRect:[outputImage extent]];
    return [UIImage imageWithCGImage:outImage];
}

@end
